package drds.binlog.binlog_event.event;

import drds.binlog.binlog_event.LogByteBlock;
import drds.binlog.binlog_event.LogEvent;
import drds.binlog.common.Authors;
import drds.common.Author;
import lombok.Getter;
import lombok.Setter;

/**
 * Append_block_log_event.
 */
@Author(name = Authors.LI_YANG)
public class AppendBlockLogEvent extends LogEvent
{

    /* AB = "Append Block" */
    public static final int ab_file_id_offset = 0;
    @Setter
    @Getter
    private final LogByteBlock logByteBlock;
    @Setter
    @Getter
    private final int logByteBlockLength;
    @Setter
    @Getter
    private final long fileId;

    public AppendBlockLogEvent(LogHeader logHeader, LogByteBlock logByteBlock, FormatDescriptionLogEvent formatDescriptionLogEvent)
    {
        super(logHeader);

        final int commonHeaderLength = formatDescriptionLogEvent.commonHeaderLength;
        final int postHeaderLength = formatDescriptionLogEvent.postHeaderLengths[logHeader.logType - 1];
        final int totalHeaderLength = commonHeaderLength + postHeaderLength;

        logByteBlock.setIndexAsOriginIndexAndOffset(commonHeaderLength + ab_file_id_offset);
        fileId = logByteBlock.getLittleEndian4UnsignedLongByIndex();

        logByteBlock.setIndexAsOriginIndexAndOffset(postHeaderLength);
        logByteBlockLength = logByteBlock.limit() - totalHeaderLength;
        this.logByteBlock = logByteBlock.duplicateByIndex(logByteBlockLength);
    }

    public final byte[] getData()
    {
        return logByteBlock.getBytesByIndex();
    }
}
